<!--********************************************************************
* Copyright© 2000 - 2025 SuperMap Software Co.Ltd. All rights reserved.
*********************************************************************-->
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title data-i18n="resources.title_routeCalculateMeasure"></title>
    <style type="text/css">
        body {
            margin: 0;
            overflow: hidden;
            background: #fff;
            width: 100%;
            height: 100%
        }

        #map {
            position: absolute;
            width: 100%;
            height: 100%;
        }

        #toolbar {
            position: absolute;
            top: 50px;
            right: 10px;
            width: 400px;
            text-align: center;
            z-index: 100;
            border-radius: 4px;
        }
    </style>
</head>
<body>
<div id="toolbar" class="panel panel-primary">
    <div class='panel-heading'>
        <h5 class='panel-title text-center' data-i18n="resources.text_routeCalculateMeasure"></h5></div>
    <div class='panel-body content'>
        <input type="button" class="btn btn-default" data-i18n="[value]resources.text_queryRoute"
               onclick="CreateRouteObj()"/>
        <input type="button" class="btn btn-default" data-i18n="[value]resources.text_queryPoint"
               onclick="RouteCalculateMeasure()"/>
        <input type="button" class="btn btn-default" data-i18n="[value]resources.text_input_value_clear"
               onclick="Remove()"/>
    </div>
</div>
<div id="map"></div>
<script type="text/javascript" include="bootstrap,widgets.alert" src="../js/include-web.js"></script>
<script type="text/javascript" exclude="iclient-classic" src="../../dist/classic/include-classic.js"></script>
<script>
    var host = window.isLocal ? window.server : "https://iserver.supermap.io";
    var map, layer, routeObj, vectorLayer, drawPoint, pointLayer, snap01, modifyFeature,
        lineStyle = {
            strokeColor: "red",
            strokeWidth: 2,
            pointRadius: 3,
            pointerEvents: "visiblePainted",
            fill: false
        },
        pointStyle = {
            fillColor: '#669933',
            fillOpacity: 0.8,
            pointRadius: 5,
            strokeColor: '#aaee77',
            strokeWidth: 3
        },
        url1 = host + "/iserver/services/map-changchun/rest/maps/长春市区图",
        url2 = host + "/iserver/services/spatialanalyst-changchun/restjsr/spatialanalyst";

    init();

    function init() {
        layer = new SuperMap.Layer.TiledDynamicRESTLayer("changchun", url1, {
            transparent: true,
            cacheEnabled: true
        }, {maxResolution: "auto"});
        vectorLayer = new SuperMap.Layer.Vector("RouteLayer");
        pointLayer = new SuperMap.Layer.Vector("pointLayer");

        snap01 = new SuperMap.Snap([vectorLayer, pointLayer], 20, 20, {actived: true});  //设置actived为true的时候，捕捉已激活。


        drawPoint = new SuperMap.Control.DrawFeature(pointLayer, SuperMap.Handler.Point);
        drawPoint.events.on({"featureadded": drawPointCompleted});
        drawPoint.snap = snap01;

        layer.events.on({"layerInitialized": addLayer});

        map = new SuperMap.Map("map", {
            allOverlays: true, controls: [
                new SuperMap.Control.ScaleLine(),
                new SuperMap.Control.Zoom(),
                new SuperMap.Control.Navigation({
                    dragPanOptions: {
                        enableKinetic: true
                    }
                }), drawPoint]
        });
        map.addControl(new SuperMap.Control.LayerSwitcher(), new SuperMap.Pixel(42, 80));
    }

    function addLayer() {
        map.addLayers([layer, vectorLayer, pointLayer]);
        map.setCenter(new SuperMap.LonLat(4503.6240321526, -3861.911472192499), 1);
    }

    //通过SQL查询路由对象
    function CreateRouteObj() {
        widgets.alert.clearAlert();
        var queryParam, queryBySQLParams, queryBySQLService;
        queryParam = new SuperMap.REST.FilterParameter({
            name: "RouteDT_road@Changchun",
            attributeFilter: "RouteID=1690"
        });
        queryBySQLParams = new SuperMap.REST.QueryBySQLParameters({
            queryParams: [queryParam]
        });
        queryBySQLService = new SuperMap.REST.QueryBySQLService(url1, {
            eventListeners: {
                "processCompleted": processCompleted,
                "processFailed": processFailed
            }
        });
        queryBySQLService.processAsync(queryBySQLParams);
    }

    //路由对象查询成功，初始化routeObj的值，同时将查询结果绘制在地图上
    function processCompleted(queryEventArgs) {

        routeObj = geo = queryEventArgs.result.recordsets[0].features[0].geometry;
        var feature = new SuperMap.Feature.Vector();
        feature.geometry = geo;
        feature.style = lineStyle;
        vectorLayer.addFeatures(feature);
        widgets.alert.showAlert(resources.msg_querySuccess, true);
    }

    //路由对象查询失败，显示提示信息
    function processFailed() {
        widgets.alert.showAlert(resources.msg_queryFailure, false);
    }

    //激活画点控件，判断routeObj是否为空，如果为空则禁止下一步操作
    function RouteCalculateMeasure() {
        widgets.alert.clearAlert();
        if (!routeObj) {
            widgets.alert.showAlert(resources.msg_getRoute, false);
            return;
        }
        drawPoint.activate();
    }

    //绘制点完成触发事件，先将点绘制在地图上，然后以点的geometry和查询结果
    // routeObj为参数初始化"点定里程"的参数类。
    function drawPointCompleted(drawGeometryArgs) {
        var feature = new SuperMap.Feature.Vector();
        var geometry = drawGeometryArgs.feature.geometry
        feature.geometry = geometry;
        feature.style = pointStyle;
        pointLayer.addFeatures(feature);

        //创建“点定里程”参数类。
        var parameters = new SuperMap.REST.RouteCalculateMeasureParameters({
            "sourceRoute": routeObj,
            "tolerance": 10,
            "point": geometry,
            "isIgnoreGap": false
        });

        //创建“点定里程”服务类。
        var routeCalculateMeasureService = new SuperMap.REST.RouteCalculateMeasureService(url2, {
                eventListeners: {
                    "processCompleted": routeCalculateMeasureCompleted,
                    'processFailed': routeCalculateMeasureFailed
                }
            }
        );
        //执行相关操作
        routeCalculateMeasureService.processAsync(parameters);
    }

    //“点定里程”查询完成，提示返回信息
    function routeCalculateMeasureCompleted(e) {
        widgets.alert.showAlert(resources.msg_MByQuery + e.result.measure, true);
    }

    //“点定里程”查询失败，提示服务端返回的信息
    function routeCalculateMeasureFailed(e) {
        widgets.alert.showAlert(e.error.errorMsg, false);
    }

    //移除图面要素，并使画点控件失效，同时将路由对象置空，去掉提示信息
    function Remove() {
        widgets.alert.clearAlert();
        vectorLayer.removeAllFeatures();
        pointLayer.removeAllFeatures();
        drawPoint.deactivate();
        routeObj = null;
    }
</script>

</body>
</html>